package com.ziytek.web.citizen.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.mapper.CommandIssuedMapper;
import com.ziytek.web.citizen.mapper.OfflineTaskTableMapper;
import com.ziytek.web.citizen.model.bean.AddSettings;
import com.ziytek.web.citizen.model.bean.IssuedIngSettings;
import com.ziytek.web.citizen.model.rsp.ExecutingStepQueryRsp;
import com.ziytek.web.citizen.service.itf.ExecutingStepQueryService;
import com.ziytek.web.citizen.util.CabinetNoUtil;
import com.ziytek.web.citizen.util.NumberToUtils;
import com.ziytek.web.citizen.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author fenghx
 * @version v1.0
 * @className ChargeControlSetQueryServiceImpl
 * @description 分容柜正在执行的工步设置查询
 * @date 2023/8/7 14:47
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExecutingStepQueryServiceImpl implements ExecutingStepQueryService {
    private final Gson gson;
    private final CommandIssuedMapper commandIssuedMapper;
    private final OfflineTaskTableMapper offlineTaskTableMapper;

    @Override
    public ExecutingStepQueryRsp executingStepQuery(String cabinetNo) {
        var response = new ExecutingStepQueryRsp();
        response.success();
        if (StringUtil.isEmpty(cabinetNo)) {
            response.error("接口请求参数address不能为空");
            return response;
        }

        cabinetNo = CabinetNoUtil.getCabinetNo(cabinetNo);
        var commandIssued = commandIssuedMapper.findByCommandAll(cabinetNo);
        // 不存在正在执行的工步方案
        if (commandIssued == null || commandIssued.getStepId() == -1) {
            response.error(String.format("未查询到[%s]号机柜正在执行的工步", cabinetNo));
            return response;
        }

        // 没有离线任务
        var offlineTaskTable = offlineTaskTableMapper.findByCabinetNo(cabinetNo);
        boolean finish = commandIssued.getFinishedStatus() == 1;
        boolean notHasOrFinish = offlineTaskTable == null && finish;
        if (notHasOrFinish) {
            response.error(String.format("未查询到[%s]号机柜正在执行的工步,工步执行已结束", cabinetNo));
            return response;
        }

        AddSettings set = gson.fromJson(commandIssued.getSettingsDetail(), new TypeToken<AddSettings>() {
        }.getType());

        var settings = new IssuedIngSettings();
        settings.setCutOff(set.getCutOff());
        settings.setSetType(set.getSetType());
        settings.setVoltage(set.getVoltage());
        settings.setCurrent(set.getCurrent());
        settings.setCutOffType(set.getCutOffType());
        settings.setExpectedTime(set.getExpectedTime());
        settings.setSettingOrder(set.getSettingOrder());

        var totalTimeHasExecuted = commandIssued.getHasExecutedTime();
        var executedTime = NumberToUtils.getDecimalPoint(StringUtil.toDouble(String.valueOf(totalTimeHasExecuted / (NumConstant.DOUBLE_SIXTY * NumConstant.DOUBLE_ONE_THOUSAND))), NumConstant.INT_TWO);
        settings.setIsFinish(finish);
        response.setIssuedIngSettings(settings);
        settings.setExecutedTime(finish ? Double.parseDouble(String.valueOf(settings.getExpectedTime())) : executedTime);

        response.setStepId(commandIssued.getStepId());
        return response;
    }
}
